package com.authine.cloudpivot.ext.dto.query;

import com.authine.cloudpivot.ext.Enum.SettleType;
import com.authine.cloudpivot.ext.Enum.TradeInvoiceStatus;
import com.authine.cloudpivot.ext.Enum.TradeSettleStatus;
import com.authine.cloudpivot.ext.enums.report.ReportType;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.experimental.SuperBuilder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Data
@SuperBuilder
@NoArgsConstructor
@Accessors(chain = true)
@Schema(description = "交易记录表相关字段查询条件")
public class TradeQuery {

    @Schema(description = "交易编号")
    private String id;

    @Schema(description = "业务方向，Sale=销售，Purchase=采购", required = true)
    private String direction;
    @Schema(description = "是否为转采订单，0=否，1=是")
    private Integer transfer_purchase;

    @Schema(description = "erp_po")
    private String erp_po;
    @Schema(description = "客户料号")
    private String sku;

    @Schema(description = "业务类型，Agent，Ordinary", required = true)
    private String business_type;

    @Schema(description = "目标组织ID")
    private String organization_id;

    @Schema(description = "目标组织名称（客户名称）")
    private String organization_name;

    @Schema(description = "创建时间")
    private List<String> createdTime;

    @Schema(description = "订单商品ID", required = true)
    private String order_goods_id;

    @Schema(description = "发票申请单编号")
    private String invoice_ids;

    @Schema(description = "发票号码")
    private String invoice_number;

    @Schema(description = "供应商(客户)名称,交易是Sale为供应商，采购则为客户")
    private String target_organization_name;

    @Schema(description = "交易摘要（品名|品牌|规格|型号|单位）")
    private String summary;

    @Schema(description = "开票状态,Undo=未开票,Doing=开票中,Done=已开票")
    private String invoice_status;

    @Schema(description = "付款状态")
    private String settle_status;

    @Schema(description = "订单商品状态")
    private Collection<String> order_goods_status;
    @Schema(description = "客户订单编号")
    private String customer_order_id;
    @Schema(description = "供应商订单编号")
    private String vendor_order_id;
    @Schema(description = "下单人")
    private String member_name;
    @Schema(description = "订单收货人")
    private String order_receiver;
    @Schema(description = "订单收货地址")
    private String order_address;
    @Schema(description = "外部工厂代码")
    private String clientFactoryCode;

    /**
     * 平台销售对应平台组织ID
     */
    @ApiModelProperty(value = "平台销售对应平台组织ID")
    private Collection<String> platform_organization_id;

    @Schema(description = "当前页")
    private Integer pageIndex=1;

    @Schema(description = "每页条数")
    private Integer pageSize=10;

    @Schema(description = "待开票查询维度。销售订单维度=ORDER,采购订单维度=VENDOR_ORDER;")
    private String queryType;

    @Schema(description = "可开票数量大于",required = true)
    private BigDecimal allowedInvoiceQuantityStart;
    @Schema(description = "反向交易结算类型")
    private SettleType target_payment_type;
    @Schema(description = "反向交易结算状态")
    private TradeSettleStatus target_settle_statuss;

    /**
     * 报表导出字段
     */
    @Schema(description = "报表类型")
    private ReportType reportType;
    @Schema(description = "报表名称")
    private String reportName;




    public String getWhereSql(Map<String, Object> param){
        StringBuffer where = new StringBuffer(" where deleted=0");
        if(StringUtils.isNotBlank(this.getDirection())){
            where.append(" and direction = :direction");
            param.put("direction", this.getDirection());
        }
        if(StringUtils.isNotBlank(id)){
            where.append(" and id in (:id)");
            param.put("id", Arrays.asList(this.getId().split(";")));
        }
        if (StringUtils.isNotBlank(this.getCustomer_order_id())){
            where.append(" and customer_order_id in (:customer_order_id)");
            param.put("customer_order_id", Arrays.asList(this.getCustomer_order_id().split(";")));
        }
        if (StringUtils.isNotBlank(this.getInvoice_number())){
            List<String> numbers = Arrays.asList(this.getInvoice_number().split(";"));
            if(numbers.size()==1){
                where.append(" and invoice_number like :invoiceNumber");
                param.put("invoiceNumber", "%"+ numbers.get(0) +"%");
            }else{
                where.append(" and ( ");
                for (int i = 0; i < numbers.size(); i++) {
                    if (i != 0) where.append(" or ");
                    where.append(" invoice_number like (:invoiceNumber"+i+")");
                    param.put("invoiceNumber"+i, numbers.get(i));

                }
                where.append(" ) ");
            }
        }
        if (StringUtils.isNotBlank(this.getOrganization_name())) {
            where.append(" and organization_name like :organization_name");
            param.put("organization_name", "%"+ this.getOrganization_name()+"%");
        }
        if (StringUtils.isNotBlank(this.getOrder_goods_id())) {
            where.append(" and order_goods_id in (:order_goods_id)");
            param.put("order_goods_id", Arrays.asList(this.getOrder_goods_id().split(";")));
        }
        if (StringUtils.isNotBlank(this.getBusiness_type())) {
            where.append(" and business_type = :business_type");
            param.put("business_type", this.getBusiness_type());
        }
        if (StringUtils.isNotBlank(this.getErp_po())) {
            where.append(" and erp_po like :erp_po");
            param.put("erp_po", "%".concat(this.getErp_po()).concat("%"));
        }
        if (StringUtils.isNotBlank(this.getSku())) {
            where.append(" and sku like :sku");
            param.put("sku", "%".concat(this.getSku()).concat("%"));
        }
        if (StringUtils.isNotBlank(this.getMember_name())) {
            where.append(" and member_name like :member_name");
            param.put("member_name", "%".concat(this.getMember_name()).concat("%"));
        }
        if (StringUtils.isNotBlank(this.getSummary())) {
            where.append(" and summary like :summary");
            param.put("summary", "%".concat(this.getSummary()).concat("%"));
        }
        if (!CollectionUtils.isEmpty(this.getOrder_goods_status())) {
            where.append(" and order_goods_status in (:order_goods_status)");
            param.put("order_goods_status", this.getOrder_goods_status());
        }
        if (StringUtils.isNotBlank(this.getOrder_receiver())) {
            where.append(" and order_receiver = :order_receiver");
            param.put("order_receiver", this.getOrder_receiver());
        }
        if (StringUtils.isNotBlank(this.getOrder_address())) {
            where.append(" and order_address = :order_address");
            param.put("order_address", this.getOrder_address());
        }
        if (StringUtils.isNotBlank(this.getClientFactoryCode())) {
            where.append(" and clientFactoryCode = :clientFactoryCode");
            param.put("clientFactoryCode", this.getClientFactoryCode());
        }
        if (!ObjectUtils.isEmpty(this.getAllowedInvoiceQuantityStart())) {
            where.append(" and allowed_invoice_quantity > :allowedInvoiceQuantityStart");
            param.put("allowedInvoiceQuantityStart", this.getAllowedInvoiceQuantityStart());
        }
        if(StringUtils.isNotBlank(this.getSettle_status())){
            where.append(" and settle_status = :settle_status");
            param.put("settle_status", this.getSettle_status());
        }
        if(StringUtils.isNotBlank(this.getInvoice_status())){
            where.append(" and invoice_status = :invoice_status");
            param.put("invoice_status", this.getInvoice_status());
        }
        if(StringUtils.isNotBlank(this.getTarget_organization_name())){
            where.append(" and target_organization_name like :target_organization_name");
            param.put("target_organization_name", "%".concat(this.getTarget_organization_name()).concat("%"));
        }
        if (!CollectionUtils.isEmpty(this.getCreatedTime())) {
            where.append(" and createdTime between :createdStart and :createdEnd");
            param.put("createdStart", this.getCreatedTime().get(0));
            param.put("createdEnd", this.getCreatedTime().get(1));
        }
        if(!CollectionUtils.isEmpty(this.getPlatform_organization_id())){
            where.append(" and JSON_UNQUOTE(platform_organization_id ->'$.id') in (:platform_organization_id)");
            param.put("platform_organization_id", this.getPlatform_organization_id());
        }
        return where.toString();

    }


}
